﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Product;
using ZBJF.Warranty.Model.ViewModel.SortingCenter;

namespace ZBJF.Warranty.BLL
{
    public partial class SortingCenterService
    {
        #region 根据主键ID获取分拣中心 + SortingCenter LoadByID(int id)
        public SortingCenter LoadByID(int id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }
        #endregion

        #region 判断分拣中心是否存在 + bool IsExist(long productTypeID, int accessoriesTypeID, string SortingCenterName)

        /// <summary>
        /// 判断分拣中心是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsExist(string sortingCenterName, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<SortingCenter>();
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID != id);
            }
            expressionWhere = expressionWhere.And(o => o.SortingCenterName.Equals(sortingCenterName));
            return LoadEntities(expressionWhere).Any();
        }
        #endregion

        #region 分页获取分拣中心列表 + IQueryable<SortingCenter> GetPagingList(SortingCenterModel model, out int total)
        /// <summary>
        /// 分页获取分拣中心列表
        /// </summary>
        /// <param name="model">过滤条件</param>
        /// <param name="total">总数量</param>
        /// <returns></returns>

        public IQueryable<SortingCenter> GetPagingList(SortingCenterModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<SortingCenter>();
            if (model.ProvinceId > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ProvinceId == model.ProvinceId);
            }
            if (model.CityId > 0)
            {
                expressionWhere = expressionWhere.And(o => o.CityId == model.CityId);
            }
            if (!string.IsNullOrEmpty(model.SortingCenterName))
            {
                expressionWhere = expressionWhere.And(o => o.SortingCenterName.Contains(model.SortingCenterName));
            }
            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "ID DESC";
            }
            return LoadPagerEntities<SortingCenter>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        #endregion

        public IQueryable<SortingCenter> GetSortingAll()
        {
            return LoadEntities(n => true).DefaultIfEmpty();
        }
    }
}
